﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;

/// <summary>
/// Summary description for ReduceBonusProcess
/// </summary>
/// 

namespace Bestlife.ERP.DataAccess
{

    public class ReduceBonusProcess
    {
        Boolean reducechild = false;
        public ReduceBonusProcess()
        {
            //
            // TODO: Add constructor logic here
            //
        }


        public Boolean childBackprocess(string dis_id, double pv, double bv, double Tprice) 
        {
            Bestlife.ERP.DataAccess.D_bonus bns = new D_bonus();
            Bestlife.ERP.Entities.E_Bonus ebns = new Entities.E_Bonus();
            ebns=bns.Getbonus(dis_id);

            #region database values 
            
            double ppvDb = double.Parse(ebns.Ppv.ToString());
            double bvDb = double.Parse(ebns.Bv.ToString());
            double TbvDb = double.Parse(ebns.Tbv.ToString());
            double TpvDb = double.Parse(ebns.Tpv.ToString());


            double CpvDb = double.Parse(ebns.Cpv.ToString());
            double Cpv_monthDb = double.Parse(ebns.CPV_month.ToString());
            double Tppv_monthDb = double.Parse(ebns.Tppv_month.ToString());
            double Tbv_monthDb = double.Parse(ebns.Tbv_month.ToString());

            double Total_PticeDb = double.Parse(ebns.T_sales.ToString());
            double TotalCommitionsDb = double.Parse(ebns.T_commision.ToString());
            string child_status = ebns.User_Status;
            double TotalCommitions=0;

            #region Direct commision

            if (child_status.Equals("2")) { TotalCommitions = TotalCommitionsDb - ((bv * 5) / 100); }
            else if (child_status.Equals("3")) { TotalCommitions = TotalCommitionsDb - ((bv * 10) / 100); }
            else if (child_status.Equals("4")) { TotalCommitions = TotalCommitionsDb - ((bv * 15) / 100); }
            else if (child_status.Equals("5")) { TotalCommitions = TotalCommitionsDb - ((bv * 20) / 100); }
            else if (child_status.Equals("6")) { TotalCommitions = TotalCommitionsDb - ((bv * 24) / 100); }
            else if (child_status.Equals("7")) { TotalCommitions = TotalCommitionsDb - ((bv * 28) / 100); }
            else if (child_status.Equals("8")) { TotalCommitions = TotalCommitionsDb - ((bv * 32) / 100); }
            else if (child_status.Equals("9")) { TotalCommitions = TotalCommitionsDb - ((bv * 36) / 100); }
            else if (child_status.Equals("10")) { TotalCommitions = TotalCommitionsDb - ((bv * 40) / 100); }
            
            #endregion


            #endregion

            double ppvUp = ppvDb - pv;
            double bvUp = bvDb - bv;
            double TbvUp = TbvDb - bv;
            double TpvUp = TpvDb - pv;
            double CpvUp = CpvDb - pv;
            double Cpv_monthUp = Cpv_monthDb - pv;
            double Total_PticeUp = Total_PticeDb - Tprice;
            double Tppv_monthUp = Tppv_monthDb - pv;
            double Tbv_monthUp = Tbv_monthDb - bv;



            BusinessRules.Check_montlyPV mp = new BusinessRules.Check_montlyPV();
            Boolean chkp = mp.check_monthlyPV(dis_id, ebns.User_Status);

            if (chkp == true)
            {

                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);
                String updtxt2 = "UPDATE Bonus SET  T_commision ='" + TotalCommitions + "',Last_Modify='" + System.DateTime.Now + "' WHERE User_id='" + dis_id + "'";
                // have to update all;
                reducechild = clsCon.AddEditDel(updtxt2);
            }
            else
            {
                Bestlife.ERP.DataAccess.D_bonus bns1 = new DataAccess.D_bonus();//(Add by chathuranga)
                Bestlife.ERP.Entities.E_Bonus ebns1 = new Entities.E_Bonus();//(Add by chathuranga)
                ebns1 = bns1.Getbonus(dis_id);
                double allbv = double.Parse(ebns1.Storebv.ToString()) - bv;
                double allpv = double.Parse(ebns1.Storepv.ToString()) - pv;
                double store_commisionDb = double.Parse(ebns1.Storecomm.ToString());
                double storecomm=0;
                #region Direct commision for store commision

                if (child_status.Equals("2")) { TotalCommitions = store_commisionDb - ((bv * 5) / 100); }
                else if (child_status.Equals("3")) { TotalCommitions = store_commisionDb - ((bv * 10) / 100); }
                else if (child_status.Equals("4")) { TotalCommitions = store_commisionDb - ((bv * 15) / 100); }
                else if (child_status.Equals("5")) { TotalCommitions = store_commisionDb - ((bv * 20) / 100); }
                else if (child_status.Equals("6")) { TotalCommitions = store_commisionDb - ((bv * 24) / 100); }
                else if (child_status.Equals("7")) { TotalCommitions = store_commisionDb - ((bv * 28) / 100); }
                else if (child_status.Equals("8")) { TotalCommitions = store_commisionDb - ((bv * 32) / 100); }
                else if (child_status.Equals("9")) { TotalCommitions = store_commisionDb - ((bv * 36) / 100); }
                else if (child_status.Equals("10")) { TotalCommitions = store_commisionDb - ((bv * 40) / 100); }

                #endregion
                

                String connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
                Bestlife.ERP.DataAccess.clsCon.connect(connStr);
                String updtxt2 = "UPDATE Bonus SET   Store_comm='" + storecomm + "', Store_bv='" + allbv + "', Store_pv='" + allpv + "',Last_Modify='" + System.DateTime.Now + "' WHERE User_id='" + dis_id + "'";
                // String updtxt2 = "UPDATE Bonus  SET  T_commision='999' WHERE User_id='001'";
                reducechild = clsCon.AddEditDel(updtxt2);
            }








            reducechild = true;
            return reducechild;
        }
    }
}